package com.wish.risk.service.impl;

import com.wish.risk.domain.SaveFkBusinessCreditForm;
import com.wish.risk.dto.CreditApartmentResponseDto;
import com.wish.risk.dto.ViewDetailsResponseDto;
import com.wish.risk.dto.ZJFInformation;
import com.wish.risk.mapper.TFkBusinessCreditMapper;
import com.wish.risk.service.ITFkBusinessCreditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 风控公寓授信主Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-06-19
 */
@Service
public class TFkBusinessCreditServiceImpl implements ITFkBusinessCreditService {


    @Autowired
    private TFkBusinessCreditMapper tFkBusinessCreditMapper;

    @Override
    public List<CreditApartmentResponseDto> findCreditApartmentList(CreditApartmentResponseDto c) {

        return tFkBusinessCreditMapper.findCreditApartmentList(c);
    }

    @Override
    public ViewDetailsResponseDto findById(ViewDetailsResponseDto viewDetailsResponseDto) {
        ViewDetailsResponseDto viewDto = new ViewDetailsResponseDto();
        List<ZJFInformation> informationList = (List<ZJFInformation>) tFkBusinessCreditMapper.findById(viewDetailsResponseDto.getBusinessId());
        int size = informationList.size();
        if(size>0) {
            viewDto.setInformation(informationList);
        }

        for (ZJFInformation zjfInformation : informationList) {
            if(zjfInformation.getMonthlyUsedCreditline() != null) {
                zjfInformation.setMonthlyCreditlineCount(zjfInformation.getMonthlyTotalCreditline().subtract(zjfInformation.getMonthlyUsedCreditline()));
            }else {
                zjfInformation.setMonthlyCreditlineCount(zjfInformation.getMonthlyTotalCreditline());
            }
            if(zjfInformation.getUsedCreditline() != null) {
                zjfInformation.setSurplusCreditlineCount(zjfInformation.getTotalCreditline().subtract(zjfInformation.getUsedCreditline()));
            }else {
                zjfInformation.setSurplusCreditlineCount(zjfInformation.getTotalCreditline());
            }
        }

        viewDto.setMonthlyCreditlineCount(viewDetailsResponseDto.getMonthlyCreditlineCount());
        viewDto.setHlTotalCreditline(viewDetailsResponseDto.getHlTotalCreditline());

        BigDecimal monthlyUsed = new BigDecimal(String.valueOf(viewDetailsResponseDto.getMonthlyUsedCount()));
        BigDecimal monthlyUse = monthlyUsed.divide(new BigDecimal(1000));
        viewDto.setMonthlyUsedCount(monthlyUse);
        viewDto.setName(viewDetailsResponseDto.getName());
        viewDto.setRegisterCapital(viewDetailsResponseDto.getRegisterCapital());
        viewDto.setRegisterDate(viewDetailsResponseDto.getRegisterDate());
        viewDto.setSurplusCreditlineCount(viewDetailsResponseDto.getSurplusCreditlineCount());
        viewDto.setHlMonthlyTotalCreditline(viewDetailsResponseDto.getHlMonthlyTotalCreditline());
        BigDecimal usedCreditline = new BigDecimal(String.valueOf(viewDetailsResponseDto.getUsedCreditlineCount()));
        BigDecimal usedCredit = usedCreditline.divide(new BigDecimal(1000));
        viewDto.setUsedCreditlineCount(usedCredit);
        viewDto.setBusinessId(viewDetailsResponseDto.getBusinessId());
        viewDto.setCreditId(viewDetailsResponseDto.getCreditId());

        return viewDto;
    }

    @Override
    public boolean update(SaveFkBusinessCreditForm saveFkBusinessCreditForm) {
        return tFkBusinessCreditMapper.update(saveFkBusinessCreditForm);
    }
}
